package com.kingyea.mobilepolice.apimanage.utils;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import org.apache.poi.ss.usermodel.Workbook;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Excel 导出
 *
 * @author Mr.Lin
 * @version v1.0.0
 * @description
 * @date Created in 2017/12/2 11:16
 */
public class ExcelUtil {

    public static Integer PAGESIZE = 50000;

    /**
     * 单种类型数据分页输出  ExcelType.HSSF输出  默认每页 50000 行
     *
     * @param dataList 待导出数据
     * @param title    标题
     * @return org.apache.poi.ss.usermodel.Workbook
     * @author Mr.Lin
     * @date 2017/12/2 11:32
     */
    public static Workbook getWorkbookMoreSheet(List dataList, String title, Class clazz) throws Exception {
        return getWorkbookMoreSheet(dataList, title, clazz, null);
    }

    /**
     * 单种类型数据分页输出  ExcelType.HSSF输出
     *
     * @param dataList 待导出数据
     * @param title    标题
     * @param pageSize 页面大小，默认50000
     * @return org.apache.poi.ss.usermodel.Workbook
     * @author Mr.Lin
     * @date 2017/12/2 11:32
     */
    public static Workbook getWorkbookMoreSheet(List dataList, String title, Class clazz, Integer pageSize) throws Exception {
        System.out.println(dataList.size());
        pageSize = NumberUtils.isNullOrLEZero(pageSize) ? 0 : PAGESIZE;
        int pageNum = 0;
        List<Map<String, Object>> exportParamsList = new ArrayList<Map<String, Object>>();
        List dataOnePage = new ArrayList<>();
        for (int i = 0; i < dataList.size(); i++) {
            dataOnePage.add(dataList.get(i));
            pageSize++;
            // XX数量 行写一页 , 或者不够一页的最后一页数据也写一页
            if (pageSize >= PAGESIZE || i + 1 == dataList.size()) {
                pageNum++;
                // final copy
                int finalPageNum = pageNum;
                List finalDataPage = dataOnePage;
                exportParamsList.add(new HashMap<String, Object>() {{
                    put("title", new ExportParams(title + "-" + finalPageNum, "sheet" + finalPageNum));
                    put("entity", clazz);
                    put("data", finalDataPage);
                }});
                // XX 行后重置数据
                pageSize = 0;
                dataOnePage = new ArrayList<>();
            }
        }
        System.out.println(exportParamsList.size());
        return ExcelExportUtil.exportExcel(exportParamsList, ExcelType.HSSF);
    }

    /**
     * 单种类型数据单页输出  ExcelType.HSSF输出  默认每页 50000 行
     *
     * @param dataList 待导出数据
     * @param title    标题
     * @return org.apache.poi.ss.usermodel.Workbook
     * @author Mr.Lin
     * @date 2017/12/2 11:32
     */
    public static Workbook getWorkbookSingleSheet(List dataList, String title, Class clazz) throws Exception {
        return ExcelExportUtil.exportExcel(new ExportParams(title, "sheet"), clazz, dataList);
    }

}